<template>
  <div id="register" v-title data-title="修改密码 - 知识漫游系统">

    <div class="me-login-box me-login-box-radius">
      <h1>知识漫游系统 修改密码</h1>

      <el-form ref="userForm" :model="userForm" :rules="rules">
        <el-form-item prop="oldPwd">
          <el-input placeholder="旧密码" type="password" v-model="userForm.oldPwd"></el-input>
        </el-form-item>

        <el-form-item prop="newPwd">
          <el-input placeholder="新密码" type="password" v-model="userForm.newPwd"></el-input>
        </el-form-item>

        <el-form-item prop="twiceNewPwd">
          <el-input placeholder="再次输入新密码" type="password" v-model="userForm.twiceNewPwd"></el-input>
        </el-form-item>

        <el-form-item size="small" class="me-login-button">
          <el-button type="primary" @click.native.prevent="changePwd('userForm')">修改</el-button>
        </el-form-item>
      </el-form>



    </div>
  </div>
</template>

<script>
  import {changePwd} from '@/api/changepwd'
  import {logout} from '@/api/login'
  import {getToken} from '@/request/token'

  export default {
    name: 'changePwd',
    data() {
      return {
        userForm: {
          oldPwd: '',
          newPwd: '',
          twiceNewPwd: ''
        },
        rules: {
          oldPwd: [
            {required: true, message: '请输入旧密码', trigger: 'blur'},
            {max: 10, message: '不能大于10个字符', trigger: 'blur'}
          ],
          newPwd: [
            {required: true, message: '请输入新密码', trigger: 'blur'},
            {max: 10, message: '不能大于10个字符', trigger: 'blur'}
          ],
          twiceNewPwd: [
            {required: true, message: '再次输入新密码', trigger: 'blur'},
            {max: 10, message: '不能大于10个字符', trigger: 'blur'}
          ]
        }

      }
    },
    methods: {
      changePwd(formName) {
        let that = this
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let loading = this.$loading({
              lock: true,
              text: '修改中，请稍后...'
            })
            changePwd(that.userForm).then((data) => {
              if(data.success){
                loading.close();
                that.$message({message: '修改成功，下次可直接用新密码登录！', type: 'success', showClose: true})
                that.$router.push({path: '/'})
              }else{
                that.$message({message: error, type: '修改失败:'+data.msg, showClose: true});
              }

            }).catch((error) => {
              loading.close();
              if (error !== 'error') {
                that.$message({message: error, type: 'error', showClose: true});
              }
            })

          } else {
            return false;
          }
        });

      },
      logout() {
        let that = this
        this.$store.dispatch('logout').then(() => {
          this.$router.push({path: '/'})
        }).catch((error) => {
          if (error !== 'error') {
            that.$message({message: error, type: 'error', showClose: true});
          }
        })
      },

    }
  }
</script>

<style scoped>
  #login {
    min-width: 100%;
    min-height: 100%;
  }

  .me-video-player {
    background-color: transparent;
    width: 100%;
    height: 100%;
    object-fit: fill;
    display: block;
    position: absolute;
    left: 0;
    z-index: 0;
    top: 0;
  }

  .me-login-box {
    position: absolute;
    width: 300px;
    height: 320px;
    background-color: white;
    margin-top: 150px;
    margin-left: -180px;
    left: 50%;
    padding: 30px;
  }

  .me-login-box-radius {
    border-radius: 10px;
    box-shadow: 0px 0px 1px 1px rgba(161, 159, 159, 0.1);
  }

  .me-login-box h1 {
    text-align: center;
    font-size: 24px;
    margin-bottom: 20px;
    vertical-align: middle;
  }

  .me-login-design {
    text-align: center;
    font-family: 'Open Sans', sans-serif;
    font-size: 18px;
  }

  .me-login-design-color {
    color: #5FB878 !important;
  }

  .me-login-button {
    text-align: center;
  }

  .me-login-button button {
    width: 100%;
  }

</style>
